package com.edu.framework.websocket.service;


import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.edu.common.constant.CacheConstants;
import com.edu.common.core.cache.ICache;
import com.edu.common.utils.bean.BeanUtils;
import com.edu.framework.websocket.WebSocketHolder;
import com.edu.framework.websocket.message.base.Response;
import com.edu.framework.websocket.message.notice.NoticeRequest;
import com.edu.framework.websocket.message.notice.NoticeVO;
import com.edu.framework.websocket.message.weather.Coordinate;
import com.edu.framework.websocket.message.weather.WeatherRequest;
import com.edu.framework.websocket.service.WeatherService;
import com.edu.system.domain.TNotice;
import com.edu.system.service.ITNoticeService;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Component("pushService")
public class PushService {

    @Autowired
    private WeatherService weatherService;

    @Autowired
    private ITNoticeService noticeService;
    @Autowired
    private ICache cache;


    public void pushWeather(){
        log.info("--- pushWeather start...");
        try {
            WebSocketHolder.broadcast(getWeather(null));
        } catch (IOException e) {
            log.error("pushWeather error: ",e);
        }
        log.info("--- pushWeather end ");
    }

    public Response<JSONObject> getWeather(WeatherRequest message){
//        String weather = "{\"aqi\":\"空气质量 102\",\"conditionId\":\"0\",\"temp\":\"晴 -5~7 ℃\"}";
//        return Response.ok(WeatherRequest.TYPE, WeatherRequest.RESP_KEY, JSONObject.parseObject(weather));
        Coordinate coordinate = Coordinate.defaultCoordinate();
        if(message != null){
            Map<String, Coordinate> data = message.getData();
            Coordinate queryCoordinate = data.get("coordinate");
            if (queryCoordinate!=null){
                coordinate = queryCoordinate;
            }
        }

        String weather = null;
        Object cacheObject = this.cache.getCacheObject(CacheConstants.WEATHER_CACHE_KEY);
        if (Objects.nonNull(cacheObject)){
            weather = cacheObject.toString();
        } else {
            weather = weatherService.getWeather(coordinate);
            this.cache.setCacheObject(CacheConstants.WEATHER_CACHE_KEY, weather, 60, TimeUnit.MINUTES);
        }

        Response<JSONObject> response = Response.ok(WeatherRequest.TYPE, WeatherRequest.RESP_KEY, JSONObject.parseObject(weather));
        return response;
    }




    public Response<JSONArray> getNotice(NoticeRequest message){
        List<TNotice> tNotices = noticeService.selectTNoticeForWS();
        if(CollectionUtils.isEmpty(tNotices)){
            return Response.ok(NoticeRequest.TYPE, NoticeRequest.RESP_KEY,new JSONArray());
        }

        List<NoticeVO> voList = convertToNoticeVO(tNotices);
        Response<JSONArray> response = Response.ok(NoticeRequest.TYPE, NoticeRequest.RESP_KEY,JSONArray.copyOf(voList));
        return response;
    }

    // 这里内容不对
    private List<NoticeVO> convertToNoticeVO(List<TNotice> notices){
        List<NoticeVO> result = new ArrayList<>(notices.size());
        for (TNotice notice : notices) {
            NoticeVO vo = new NoticeVO();
            BeanUtils.copyBeanProp(vo,notice);
            result.add(vo);
        }
        return result;
    }

}
